﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Custom.Framework
{
    public class DragWindow
    {
        private Form sForm;
        private bool isMouseDownHeader;
        private Point preMousePosition;

        public void BindEvent(Form from, params Control[] controls)
        {
            sForm = from;

            foreach (Control item in controls)
            {
                item.MouseDown += Item_MouseDown;
                item.MouseMove += Item_MouseMove;
                item.MouseUp += Item_MouseUp;
            }
        }

        private void Item_MouseUp(object sender, MouseEventArgs e)
        {
            if (this.isMouseDownHeader) this.isMouseDownHeader = false;

            sForm.Cursor = Cursors.Default;
        }

        private void Item_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.isMouseDownHeader)
            {
                sForm.Top += (e.Y - this.preMousePosition.Y);
                sForm.Left += (e.X - this.preMousePosition.X);
            }
        }

        private void Item_MouseDown(object sender, MouseEventArgs e)
        {
            this.isMouseDownHeader = true;
            this.preMousePosition = new Point(e.X, e.Y);

            sForm.Cursor = Cursors.Hand;
        }        
    }
}
